package com.infore.airsupervise.ui.fragment;

import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.beardedhen.androidbootstrap.AwesomeTextView;
import com.beardedhen.androidbootstrap.font.FontAwesome;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.chad.library.adapter.base.listener.OnItemClickListener;
import com.infore.airsupervise.R;
import com.infore.airsupervise.app.Constants;
import com.infore.airsupervise.bean.MenuE;
import com.infore.airsupervise.other.listener.MainFragBtnClickListener;
import com.infore.airsupervise.ui.widget.ItemDecoration1px;
import com.infore.airsupervise.utils.JumpUtil;
import com.infore.airsupervise.utils.ToastUtil;


import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * 首页事件模块fragment
 * Created by xiaotao on 2017/8/15.
 */

public class EventsFragment extends BaseFragment{

    @BindView(R.id.titlebar_left_menu)
    AwesomeTextView titlebarLeftMenu;
    @BindView(R.id.titlebar_title)
    TextView titlebarTitle;
    @BindView(R.id.titlebar_right_menu)
    AwesomeTextView titlebarRightMenu;
    @BindView(R.id.real_time_recycler_view)
    RecyclerView recyclerView;


    private BaseQuickAdapter<MenuE, BaseViewHolder> quickAdapter;
    private MainFragBtnClickListener listener;

    /**
     * set抽屉监听
     * @param listener
     */
    public void setMainFragBtnClickListener(MainFragBtnClickListener listener){
        this.listener = listener;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_real_time, container, false);
        unbinder = ButterKnife.bind(this, view);
        initView();
        return view;
    }

    private void initView(){
        titlebarLeftMenu.setFontAwesomeIcon(FontAwesome.FA_USER);
        titlebarTitle.setText("事件");
        titlebarRightMenu.setVisibility(View.INVISIBLE);
        // 适配器
        quickAdapter = new BaseQuickAdapter<MenuE, BaseViewHolder>(
                R.layout.item_main_menu_rv, MenuE.getEventsMenuList()) {

            @Override
            protected void convert(BaseViewHolder helper, MenuE item) {
                helper.setImageResource(R.id.menu_icon, item.getIcon())
                        .setText(R.id.menu_title, item.getTitle())
                        .setText(R.id.menu_desc, item.getDesc());
            }
        };

        // 设置recyclerView
        recyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
        recyclerView.addItemDecoration(new ItemDecoration1px(getContext(), ItemDecoration1px.VERTICAL_LIST));
        recyclerView.setAdapter(quickAdapter);
        // 设置Item点击监听
        recyclerView.addOnItemTouchListener(new OnItemClickListener() {
            @Override
            public void onSimpleItemClick(BaseQuickAdapter adapter, View view, int position) {
                MenuE menu = (MenuE) adapter.getItem(position);
                if (menu.getFirstActivity() == null) {
                    ToastUtil.showShort(getContext(), "" + position);
                    return;
                }
                JumpUtil.jumpFromTo(getContext(), menu.getFirstActivity(),Constants.INTENT_OBJ_KEY, menu);
            }
        });
    }

    /**
     * 展示抽屉布局
     */
    @OnClick(R.id.titlebar_left_menu)
    void leftMenuClick() {
        if (listener != null) {
            listener.onShowDrawerBtnClick();
        }
    }

}
